$(document).ready(function(){
	$("#jqwCloseBtn").on("click", function(){
		var jqw = new jqWindow();
		jqw.destroyJqwSelected();
	});

	$("#jqwFormReviseSaveBtn").on("click", function(){
		var cdTpRegExprVal = $.trim($("#cdTpRegExpr").val());
		var data = {isRegExpr:((cdTpRegExprVal != "") ? true : false)};
		
		if(!validationForm(data)) return;

		var chkFields = new Object();
		chkFields["actionType"] = "revise";
		chkFields["cdTpId"] = $("#cdTpId").val();
		checkDuplication.existCdTpId(chkFields, dwrResult);
	});
	
	$("#jqwFormSaveBtn").on("click", function(){
		var cdTpRegExprVal = $.trim($("#cdTpRegExpr").val());
		var data = {isRegExpr:((cdTpRegExprVal != "") ? true : false)};
		
		if(!validationForm(data)) return;
		
		var chkFields = new Object();
		chkFields["actionType"] = "new";
		chkFields["cdTpId"] = $("#cdTpId").val();
		checkDuplication.existCdTpId(chkFields, dwrResult);
	});
});

function dwrResult(data){
	var jHelper = new jsonHelper2();
	var json = jHelper.stringToJsonObj(data);

	if(!json.result){
		var url = "/template/system/codeType/"+((json.actionType == "new")?"_regFormSave":"_regFormReviseSave");
		var options = new Object();
		options["url"] = url;
		jqwSave(options);
		
	}else{
		alert(json.msg);
		$("#"+json.chkField).focus();
		return;
	}
}

function validationForm(data){
	var def = new validDefinition(data);
	var validator = new formValidator("frmCdTpReg", def.rules, def.messages);
	return validator.validation();
}

function jqwSave(opts){
	var obj = $(".pageNavi").find(".numberingOn");
	naviNumClickEventReg(obj);
	
	var options = new Object();
	options["url"] = opts["url"];
	options["type"] = "POST";
	options["dataType"] = "json";
	options["data"] = "frm:frmCdTpReg";	//Available no writing this option OR pram1=?&param2=? OR frm:form Id
	options["triggerObj"] = obj;	//트리 메뉴에서 현재 화면을 triggering(to click) 할 수 있는 오브젝트.
	options["jqwType"] = "save";	//< "popup" | "save" | [null | ""] >
	options["jqwClose"] = true;
	
	var jqw = new jqWindow();
	jqw.jqwSet(options);
	jqw.loadJqWindow();
}
